package com.example.finaltask.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.finaltask.Fragment.PostFragment;
import com.example.finaltask.modle.User;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DBHelper extends SQLiteOpenHelper {

    //数据库版本号
    public static final int DATABASE_VERSION=7;

    //数据库名
    public static final String DATABASE_NAME="murmur.db";

    //数据库中的表（User,）的名称
    private static final String USER="user";

    //声明SQLiteDataBase对象
    //private SQLiteDatabase sqLiteDatabase;

    /**
     * 需要添加到表里的字段
     */
    //private Context mContext;
    public DBHelper(Context context, String databaseName,
                    SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        //mContext = context;
    }


    /**
     * 数据库第一次创建时调用
     * */
    @Override
    public void onCreate(SQLiteDatabase db) {
        //建表
        System.out.println("创建表");
        //创建用户表
         String CREATE_USER= "create table user("+
         "id integer primary key AUTOINCREMENT,"+
         "email text,"+
         "headimage text,"+
         "password text,"+
         "username text)";
         db.execSQL(CREATE_USER);


    }
    /**
     * 数据库升级时调用
     * 当数据库版本更新执行该方法
     * */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch (newVersion) {
            case 6:
                //创建动态表
                String CREATE_POSTER = "create table poster(" +
                        "id integer primary key AUTOINCREMENT," +
                        "content text," +
                        "createtime integer," +
                        "previewimageurl text," +
                        "title text)";
                db.execSQL(CREATE_POSTER);
            case 7:
                db.execSQL("ALTER TABLE poster ADD COLUMN userid INTEGER DEFAULT null");
        }
    }

    }

